package top.lyunk.demo.server.a;

import com.alibaba.csp.sentinel.slots.block.RuleConstant;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.spring.ReferenceBean;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.context.annotation.Bean;
import top.lyunk.demo.server.b.rpc.dubbo.dubbo.ServerBDubboAPI;

import java.util.List;

@EnableDubbo
@SpringBootApplication
@EnableDiscoveryClient
@MapperScan("top.lyunk.demo.server.a.mapper")
public class ServerA {

    @Bean
    @DubboReference(interfaceClass = ServerBDubboAPI.class)
    public ReferenceBean<ServerBDubboAPI> serverBDubboAPI() {
        return new ReferenceBean<>();
    }

    public static void main(String[] args) {
        initFlowRule();
        SpringApplication.run(ServerA.class, args);
    }

    private static void initFlowRule() {
        FlowRule flowRule = new FlowRule();
        flowRule.setResource("top.lyunk.demo.server.b.rpc.dubbo.dubbo.ServerBDubboAPI:echo(java.lang.String)");
        flowRule.setCount(1);
        flowRule.setGrade(RuleConstant.FLOW_GRADE_QPS);
        flowRule.setLimitApp("default");


        FlowRule flowRule2 = new FlowRule();
        flowRule2.setResource("getTestUserId");
        flowRule2.setCount(1);
        flowRule2.setGrade(RuleConstant.FLOW_GRADE_QPS);
        flowRule2.setLimitApp("default");

        FlowRule flowRule3 = new FlowRule();
        flowRule3.setResource("/echo/*");
        flowRule2.setCount(1);
        flowRule2.setGrade(RuleConstant.FLOW_GRADE_QPS);
        flowRule2.setLimitApp("default");


        FlowRuleManager.loadRules(List.of(flowRule, flowRule2));
    }
}